#include "trace/trace-driver.hpp"

/**
 * \file
 * \ingroup trace
 * cas::TraceDriver implementation.
 */

namespace cas
{
    SLOG_COMPONENT_DEFINE("TraceDriver");

    TraceDriver::TraceDriver(std::string path) : traceFilePath(path)
    {
        SLOG_FUNCTION(path);
    }

    TraceDriver::~TraceDriver()
    {
        SLOG_FUNCTION_NOARGS();
    }

    void TraceDriver::setPath(std::string path)
    {
        SLOG_FUNCTION(path);
        traceFilePath = path;
    }

    void TraceDriver::readFile()
    {
        SLOG_FUNCTION_NOARGS();
        std::string line;
        std::ifstream fin(traceFilePath); // in stream
        std::istringstream sin;           // line stream
        int32_t emptyLineCount = 0;

        if (!fin)
        {
            SABORT_MSG("File opening failed: " << traceFilePath);
        }

        while (!fin.eof())
        {
            std::getline(fin, line);

            if (line.size() == 0)
            {
                emptyLineCount++; // encounter an empty line
                continue;
            }

            for (size_t pos = 0; pos < line.size(); pos++)
            {
                if (line[pos] == ',')
                {
                    line[pos] = ' '; // replace ',' to ' '
                }
            }

            sin.clear();
            sin.str(line);
            if (!parseLine(sin))
            {
                SFATAL_ERROR_CONT("Line parsing failed");
            }
        }

        fin.close(); // read file ends

        if (emptyLineCount > 0)
        {
            SLOG_WARN("Parsing with" << emptyLineCount << "empty line(s)");
        }
    }

    size_t TraceDriver::dataSize()
    {
        return 0;
    }

    bool TraceDriver::parseLine(std::istringstream &sin)
    {
        SLOG_FUNCTION_NOARGS();
        return true;
    }

} // namespace cas
